package com.trkj.usercenter.star;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.lidroid.xutils.BitmapUtils;
import com.trkj.base.BaseDataRefreshFragment;
import com.trkj.base.Constants;
import com.trkj.taoruiyun.R;
import com.trkj.widget.refresh.SwipeRefreshLayout;

import java.text.MessageFormat;

/**
 * 我的提问
 * Created by LiZanhong on 2015/06/25.
 */
public class AllStarFragment extends BaseDataRefreshFragment{

    @Override
    public String getUrl() {
        String url = MessageFormat.format(
                Constants.ApiUrl.ALL_STAR,
                getPage(),
                getRows());
        Log.i("URL", url);
        return url;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //先定义Adapter
        if(getAdapter() == null) {
            setAdapter(new StarAdapter());
        }
        View root = super.onCreateView(inflater, container, savedInstanceState);
        return root;
    }

    @Override
    protected SwipeRefreshLayout.Mode getMode() {
        return SwipeRefreshLayout.Mode.BOTH;
    }

    class StarAdapter extends BaseAdapter{
        @Override
        public int getCount() {
            return getData().size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;

            if(convertView == null){
                convertView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_star_item, null);
                viewHolder = new ViewHolder();
                viewHolder.photo = (ImageView)convertView.findViewById(R.id.photo);
                viewHolder.title = (TextView)convertView.findViewById(R.id.title);
                viewHolder.userName = (TextView)convertView.findViewById(R.id.user_name);
                viewHolder.starDate = (TextView)convertView.findViewById(R.id.star_date);
                viewHolder.reason = (TextView)convertView.findViewById(R.id.reason);

                convertView.setTag(viewHolder);
            }else{
                viewHolder = (ViewHolder)convertView.getTag();
            }

            JSONObject jsonObject = getData().getJSONObject(position);
            BitmapUtils bitmapUtils = new BitmapUtils(getActivity());
            bitmapUtils.display(viewHolder.photo, Constants.ApiUrl.HTTP_GSON_CN + "/upload/"
                    + jsonObject.getString("image_file"));

            viewHolder.title.setText(jsonObject.getString("title_"));
            viewHolder.userName.setText("获奖人：" + jsonObject.getString("user_name"));
            viewHolder.starDate.setText("获奖日期：" + jsonObject.getString("star_date").substring(0, 10));
            viewHolder.reason.setText(jsonObject.getString("reason_"));

            return convertView;
        }

        class ViewHolder{
            ImageView photo;
            TextView title;
            TextView userName;
            TextView starDate;
            TextView reason;
        }
    }
}
